using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Catastrophe.Common.Common;
using Catastrophe.Common.Extensions.BaseType;

namespace Catastrophe.Common.Extensions
{
	public static class StringExtensions
	{
		public static bool IsMatch(this string value, string pattern)
		{
			if (value == null)
			{
				return false;
			}
			return Regex.IsMatch(value, pattern);
		}

		public static bool IsMatch(this string value, string pattern, RegexOptions options)
		{
			if (value == null)
			{
				return false;
			}
			return Regex.IsMatch(value, pattern, options);
		}

		public static string GetMatch(this string value, string pattern)
		{
			if (value.IsEmpty())
			{
				return string.Empty;
			}
			return Regex.Match(value, pattern).Value;
		}

		public static IEnumerable<string> GetMatchingValues(this string value, string pattern)
		{
			if (value.IsEmpty())
			{
				return new string[0];
			}
			return value.GetMatchingValues(pattern, RegexOptions.None);
		}

		public static IEnumerable<string> GetMatchingValues(this string value, string pattern, RegexOptions options)
		{
			return from Match match in value.GetMatches(pattern, options)
				where match.Success
				select match.Value;
		}

		public static MatchCollection GetMatches(this string value, string pattern, RegexOptions options)
		{
			return Regex.Matches(value, pattern, options);
		}

		public static string[] RegexSplit(this string value, string pattern, RegexOptions options)
		{
			return Regex.Split(value, pattern, options);
		}

		public static string[] GetWords(this string value)
		{
			return value.RegexSplit("\\W", RegexOptions.None);
		}

		public static string GetWordByIndex(this string value, int index)
		{
			string[] words = value.GetWords();
			if (index < 0 || index > words.Length - 1)
			{
				throw new IndexOutOfRangeException("The word number is out of range.");
			}
			return words[index];
		}

		public static string SpaceOnUpper(this string value)
		{
			return Regex.Replace(value, "([A-Z])(?=[a-z])|(?<=[a-z])([A-Z]|[0-9]+)", " $1$2").TrimStart();
		}

		public static IEnumerable<string> ExtractArguments(this string value, string template, ComparsionTemplateOptions compareTemplateOptions = ComparsionTemplateOptions.Default, RegexOptions regexOptions = RegexOptions.None)
		{
			return from g in value.ExtractGroupArguments(template, compareTemplateOptions, regexOptions)
				select g.Value;
		}

		public static IEnumerable<Group> ExtractGroupArguments(this string value, string template, ComparsionTemplateOptions compareTemplateOptions = ComparsionTemplateOptions.Default, RegexOptions regexOptions = RegexOptions.None)
		{
			string regexPattern = StringExtensions.GetRegexPattern(template, compareTemplateOptions);
			Regex regex = new Regex(regexPattern, regexOptions);
			Match match = regex.Match(value);
			return match.Groups.Cast<Group>().Skip(1);
		}

		private static string GetRegexPattern(string template, ComparsionTemplateOptions compareTemplateOptions)
		{
			template = template.ReplaceAll(new string[9] { "\\", "^", "$", "*", "+", "?", ".", "(", ")" }, (string v) => "\\" + v);
			bool flag = compareTemplateOptions == ComparsionTemplateOptions.FromStart || compareTemplateOptions == ComparsionTemplateOptions.Whole;
			bool flag2 = compareTemplateOptions == ComparsionTemplateOptions.AtTheEnd || compareTemplateOptions == ComparsionTemplateOptions.Whole;
			StringBuilder stringBuilder = new StringBuilder();
			if (flag)
			{
				stringBuilder.Append("^");
			}
			stringBuilder.Append(Regex.Replace(template, "\\{[0-9]+\\}", delegate(Match m)
			{
				string s = m.ToString().Replace("{", "").Replace("}", "");
				return $"(?<{int.Parse(s) + 1}>.*?)";
			}));
			if (flag2 || ((int)template.LastOrDefault() == 125 && compareTemplateOptions == ComparsionTemplateOptions.Default))
			{
				stringBuilder.Append("$");
			}
			return stringBuilder.ToString();
		}

		public static string ReplaceWith(this string value, string pattern, string replaceValue)
		{
			return value.ReplaceWith(pattern, replaceValue, RegexOptions.None);
		}

		public static string ReplaceWith(this string value, string pattern, string replaceValue, RegexOptions options)
		{
			return Regex.Replace(value, pattern, replaceValue, options);
		}

		public static string ReplaceWith(this string value, string pattern, MatchEvaluator evaluator)
		{
			return value.ReplaceWith(pattern, RegexOptions.None, evaluator);
		}

		public static string ReplaceWith(this string value, string pattern, RegexOptions options, MatchEvaluator evaluator)
		{
			return Regex.Replace(value, pattern, evaluator, options);
		}

		public static bool IsNullOrEmpty(this string value)
		{
			return string.IsNullOrEmpty(value);
		}

		public static string IfEmpty(this string value, string defaultValue)
		{
			return (!value.IsNullOrEmpty()) ? value : defaultValue;
		}

		public static string ExceptionIfNullOrEmpty(this string value, string message, string name)
		{
			if (string.IsNullOrEmpty(value))
			{
				throw new ArgumentException(message, name);
			}
			return value;
		}

		public static bool IsImageFile(this string fileName)
		{
			if (!File.Exists(fileName))
			{
				return false;
			}
			byte[] array = File.ReadAllBytes(fileName);
			if (array.Length == 0)
			{
				return false;
			}
			switch (BitConverter.ToUInt16(array, 0))
			{
			case 18759:
			case 19778:
			case 20617:
			case 55551:
				return true;
			default:
				return false;
			}
		}

		public static bool Contains(this string inputValue, string comparisonValue, StringComparison comparisonType)
		{
			return inputValue.IndexOf(comparisonValue, comparisonType) != -1;
		}

		public static bool ContainsEquivalenceTo(this string inputValue, string comparisonValue)
		{
			return StringExtensions.BothStringsAreEmpty(inputValue, comparisonValue) || StringExtensions.StringContainsEquivalence(inputValue, comparisonValue);
		}

		private static bool BothStringsAreEmpty(string inputValue, string comparisonValue)
		{
			return inputValue.IsEmpty() && comparisonValue.IsEmpty();
		}

		private static bool StringContainsEquivalence(string inputValue, string comparisonValue)
		{
			return !inputValue.IsEmpty() && inputValue.Contains(comparisonValue, StringComparison.InvariantCultureIgnoreCase);
		}

		public static bool ContainsAny(this string value, params string[] values)
		{
			return value.ContainsAny(StringComparison.CurrentCulture, values);
		}

		public static bool ContainsAny(this string value, StringComparison comparisonType, params string[] values)
		{
			return values.Any((string v) => value.IndexOf(v, comparisonType) > -1);
		}

		public static bool ContainsAll(this string value, params string[] values)
		{
			return value.ContainsAll(StringComparison.CurrentCulture, values);
		}

		public static bool ContainsAll(this string value, StringComparison comparisonType, params string[] values)
		{
			return values.All((string v) => value.IndexOf(v, comparisonType) > -1);
		}

		public static bool IsNumeric(this string value)
		{
			float result;
			return float.TryParse(value, out result);
		}

		public static bool EquivalentTo(this string value, string whateverCaseString, StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
		{
			return string.Equals(value, whateverCaseString, comparison);
		}

		public static bool EqualsAny(this string value, StringComparison comparisonType, params string[] values)
		{
			return values.Any((string v) => value.Equals(v, comparisonType));
		}

		public static bool IsLikeAny(this string value, params string[] patterns)
		{
			return patterns.Any(new Func<string, bool>(value.IsLike));
		}

		public static bool IsLike(this string value, string pattern)
		{
			if (value == pattern)
			{
				return true;
			}
			if (pattern[0] == '*' && pattern.Length > 1)
			{
				return value.Where((char t, int index) => value.Substring(index).IsLike(pattern.Substring(1))).Any();
			}
			if (pattern[0] == '*')
			{
				return true;
			}
			if (pattern[0] == value[0])
			{
				return value.Substring(1).IsLike(pattern.Substring(1));
			}
			return false;
		}

		public static Func<bool> IsItemInEnum<TEnum>(this string value) where TEnum : struct
		{
			return () => string.IsNullOrEmpty(value) || !Enum.IsDefined(typeof(TEnum), value);
		}

		public static bool IsRangeLength(this string source, int minLength, int maxLength)
		{
			return source.Length >= minLength && source.Length <= maxLength;
		}

		public static string Remove(this string value, params char[] removeChar)
		{
			if (!string.IsNullOrEmpty(value) && removeChar != null)
			{
				Array.ForEach(removeChar, delegate(char c)
				{
					value = value.Remove(c.ToString());
				});
			}
			return value;
		}

		public static string Remove(this string value, params string[] strings)
		{
			return strings.Aggregate(value, (string current, string c) => current.Replace(c, string.Empty));
		}

		public static string Remove(this string value, int index, bool isLeft = true)
		{
			if (value.Length <= index)
			{
				return "";
			}
			if (isLeft)
			{
				return value.Substring(index);
			}
			return value.Substring(0, value.Length - index);
		}

		public static string RemoveAllSpecialCharacters(this string value)
		{
			StringBuilder stringBuilder = new StringBuilder(value.Length);
			foreach (char item in value.Where(new Func<char, bool>(char.IsLetterOrDigit)))
			{
				stringBuilder.Append(item);
			}
			return stringBuilder.ToString();
		}

		public static string RemoveEnd(this string value, string defaultChar = ",")
		{
			if (!value.IsNullOrEmpty() && value.EndsWith(defaultChar))
			{
				value = value.Substring(0, value.LastIndexOf(defaultChar, StringComparison.Ordinal));
			}
			return value;
		}

		public static string Remove(this string str, string tag, RegexOptions options = RegexOptions.None)
		{
			if (string.IsNullOrWhiteSpace(str))
			{
				return string.Empty;
			}
			return tag.IsNullOrEmpty() ? str : Regex.Replace(str, tag, "", options);
		}

		public static string FormatWith(this string format, params object[] args)
		{
			format.CheckNotNull("format");
			return string.Format(CultureInfo.CurrentCulture, format, args);
		}

		public static string ReverseString(this string value)
		{
			value.CheckNotNull("value");
			return new string(value.Reverse().ToArray());
		}

		public static string[] Split(this string value, string strSplit, bool removeEmptyEntries = false)
		{
			return value.Split(new string[1] { strSplit }, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
		}

		public static int GetTextLength(this string value)
		{
			ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = aSCIIEncoding.GetBytes(value);
			byte[] array = bytes;
			foreach (byte b in array)
			{
				num = ((b != 63) ? (num + 1) : (num + 2));
			}
			return num;
		}

		public static string TrimToMaxLength(this string value, int maxLength)
		{
			return (value == null || value.Length <= maxLength) ? value : value.Substring(0, maxLength);
		}

		public static string TrimToMaxLength(this string value, int maxLength, string suffix)
		{
			return (value == null || value.Length <= maxLength) ? value : (value.Substring(0, maxLength) + suffix);
		}

		public static string Truncate(this string value, int length, bool userElipse = false)
		{
			int num = (userElipse ? 3 : 0);
			if (length - num <= 0)
			{
				throw new InvalidOperationException($"Length must be greater than {num}.");
			}
			if (value.IsNullOrEmpty() || value.Length <= length)
			{
				return value;
			}
			return value.Substring(0, length - num) + new string('.', num);
		}

		public static string PadBoth(this string value, int width, char padChar, bool truncate = false)
		{
			int num = width - value.Length;
			if (num == 0 || (num < 0 && !truncate))
			{
				return value;
			}
			if (num < 0)
			{
				return value.Substring(0, width);
			}
			return value.PadLeft(width - num / 2, padChar).PadRight(width, padChar);
		}

		public static string EnsureStartsWith(this string value, string prefix)
		{
			return value.StartsWith(prefix) ? value : (prefix + value);
		}

		public static string EnsureEndWith(this string value, string suffix)
		{
			return value.EndsWith(suffix) ? value : (value + suffix);
		}

		public static string Repeat(this string value, int repeatCount)
		{
			if (value.Length == 1)
			{
				return new string(value[0], repeatCount);
			}
			StringBuilder stringBuilder = new StringBuilder(repeatCount * value.Length);
			while (repeatCount-- > 0)
			{
				stringBuilder.Append(value);
			}
			return stringBuilder.ToString();
		}

		public static string ExtractNumber(this string value)
		{
			return value.Where(new Func<char, bool>(char.IsDigit)).Aggregate(new StringBuilder(value.Length), (StringBuilder sb, char c) => sb.Append(c)).ToString();
		}

		public static string ConcatWith(this string value, params string[] values)
		{
			return string.Concat(value, string.Concat(values));
		}

		public static string Join<T>(this string value, string separator, T[] obj)
		{
			if (obj == null || obj.Length == 0)
			{
				return value;
			}
			if (separator == null)
			{
				separator = string.Empty;
			}
			Converter<T, string> converter = (T o) => o.ToString();
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(value);
			stringBuilder.Append(separator);
			stringBuilder.Append(string.Join(separator, Array.ConvertAll(obj, converter)));
			return stringBuilder.ToString();
		}

		public static string JoinNotNullOrEmpty(this string[] values, string separator)
		{
			List<string> list = values.Where((string s) => !string.IsNullOrEmpty(s)).ToList();
			return string.Join(separator, list.ToArray());
		}

		public static string GetBefore(this string value, string x)
		{
			int num = value.IndexOf(x, StringComparison.Ordinal);
			return (num == -1) ? string.Empty : value.Substring(0, num);
		}

		public static string GetBetween(this string value, string x, string y)
		{
			int num = value.IndexOf(x, StringComparison.Ordinal);
			int num2 = value.LastIndexOf(y, StringComparison.Ordinal);
			if (num == -1 || num2 == -1)
			{
				return string.Empty;
			}
			int num3 = num + x.Length;
			return (num3 >= num2) ? string.Empty : value.Substring(num3, num2 - num3).Trim();
		}

		public static string GetAfter(this string value, string x)
		{
			int num = value.IndexOf(x, StringComparison.Ordinal);
			if (num == -1)
			{
				return string.Empty;
			}
			int num2 = num + x.Length;
			return (num2 >= value.Length) ? string.Empty : value.Substring(num2).Trim();
		}

		public static string Left(this string value, int leftLength)
		{
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			if (leftLength >= value.Length)
			{
				throw new ArgumentOutOfRangeException("leftLength", leftLength, "leftLength must be less than length of string");
			}
			return value.Substring(0, leftLength);
		}

		public static string Right(this string value, int rightLength)
		{
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			if (rightLength >= value.Length)
			{
				throw new ArgumentOutOfRangeException("rightLength", rightLength, "rightLength must be less than length of string");
			}
			return value.Substring(value.Length - rightLength);
		}

		public static string SubstringFrom(this string value, int index)
		{
			return (index < 0 && index < value.Length) ? value : value.Substring(index, value.Length - index);
		}

		public static string ToUpperFirstLetter(this string value)
		{
			return StringExtensions.ToFirstLetter(value);
		}

		public static string ToLowerFirstLetter(this string value)
		{
			return StringExtensions.ToFirstLetter(value, isUpper: false);
		}

		private static string ToFirstLetter(string value, bool isUpper = true)
		{
			if (value.IsEmpty())
			{
				return string.Empty;
			}
			char[] array = value.ToCharArray();
			if (isUpper)
			{
				array[0] = char.ToUpper(array[0]);
			}
			else
			{
				array[0] = char.ToLower(array[0]);
			}
			return new string(array);
		}

		public static string ToTitleCase(this string value)
		{
			return value.ToTitleCase(Const.DefaultCultureInfo);
		}

		public static string ToTitleCase(this string value, CultureInfo culture)
		{
			return culture.TextInfo.ToTitleCase(value);
		}

		public static string ToPlural(this string singular)
		{
			int num = singular.LastIndexOf(" of ", StringComparison.Ordinal);
			if (num > 0)
			{
				return singular.Substring(0, num) + singular.Remove(0, num).ToPlural();
			}
			if (singular.EndsWith("sh") || singular.EndsWith("ch") || singular.EndsWith("us") || singular.EndsWith("ss"))
			{
				return singular + "es";
			}
			if (singular.EndsWith("y"))
			{
				return singular.Remove(singular.Length - 1, 1) + "ies";
			}
			if (singular.EndsWith("o"))
			{
				return singular.Remove(singular.Length - 1, 1) + "oes";
			}
			return singular + "s";
		}

		public static string ReplaceAll(this string value, IEnumerable<string> oldValues, Func<string, string> replacePredicate)
		{
			StringBuilder stringBuilder = new StringBuilder(value);
			foreach (string oldValue in oldValues)
			{
				string newValue = replacePredicate(oldValue);
				stringBuilder.Replace(oldValue, newValue);
			}
			return stringBuilder.ToString();
		}

		public static string ReplaceAll(this string value, IEnumerable<string> oldValues, string newValue)
		{
			StringBuilder stringBuilder = new StringBuilder(value);
			foreach (string oldValue in oldValues)
			{
				stringBuilder.Replace(oldValue, newValue);
			}
			return stringBuilder.ToString();
		}

		public static string ReplaceAll(this string value, IEnumerable<string> oldValues, IEnumerable<string> newValues)
		{
			StringBuilder stringBuilder = new StringBuilder(value);
			IEnumerator<string> enumerator = newValues.GetEnumerator();
			foreach (string oldValue in oldValues)
			{
				if (!enumerator.MoveNext())
				{
					throw new ArgumentOutOfRangeException("newValues", "newValues sequence is shorter than oldValues sequence");
				}
				stringBuilder.Replace(oldValue, enumerator.Current);
			}
			if (enumerator.MoveNext())
			{
				throw new ArgumentOutOfRangeException("newValues", "newValues sequence is longer than oldValues sequence");
			}
			return stringBuilder.ToString();
		}

		public static StringDictionary ParseCommandlineParams(this string[] value)
		{
			StringDictionary stringDictionary = new StringDictionary();
			Regex regex = new Regex("^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
			Regex regex2 = new Regex("^['\"]?(.*?)['\"]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
			string text = null;
			foreach (string input in value)
			{
				string[] array = regex.Split(input, 3);
				switch (array.Length)
				{
				case 1:
					if (text != null)
					{
						if (!stringDictionary.ContainsKey(text))
						{
							array[0] = regex2.Replace(array[0], "$1");
							stringDictionary.Add(text, array[0]);
						}
						text = null;
					}
					break;
				case 2:
					if (text != null && !stringDictionary.ContainsKey(text))
					{
						stringDictionary.Add(text, "true");
					}
					text = array[1];
					break;
				case 3:
					if (text != null && !stringDictionary.ContainsKey(text))
					{
						stringDictionary.Add(text, "true");
					}
					text = array[1];
					if (!stringDictionary.ContainsKey(text))
					{
						array[2] = regex2.Replace(array[2], "$1");
						stringDictionary.Add(text, array[2]);
					}
					text = null;
					break;
				}
			}
			if (text != null && !stringDictionary.ContainsKey(text))
			{
				stringDictionary.Add(text, "true");
			}
			return stringDictionary;
		}

		public static TEnum ParseStringToEnum<TEnum>(this string value, bool ignorecase = false) where TEnum : struct
		{
			return value.IsItemInEnum<TEnum>()() ? default(TEnum) : ((TEnum)Enum.Parse(typeof(TEnum), value, ignorecase));
		}

		public static string EncodeEmailAddress(this string emailAddress)
		{
			string text = emailAddress;
			for (int num = text.Length; num >= 1; num--)
			{
				int num2 = Convert.ToInt32(text[num - 1]);
				string text2 = num2 switch
				{
					32 => " ", 
					34 => "\"", 
					38 => "&", 
					60 => "<", 
					62 => ">", 
					_ => (num2 < 32 || num2 > 127) ? ("&#" + Convert.ToString(num2) + ";") : ("&#" + Convert.ToString(num2) + ";"), 
				};
				if (text2.Length > 0)
				{
					text = text.Substring(0, num - 1) + text2 + text.Substring(num);
				}
			}
			return text;
		}

		public static string RepairZero(this string text, int limitedLength)
		{
			return text.PadLeft(limitedLength, '0');
		}

		public static byte[] ToBytes(this string value, Encoding encoding = null)
		{
			encoding = encoding ?? Encoding.UTF8;
			return encoding.GetBytes(value);
		}

		public static XDocument ToXDocument(this string xml)
		{
			return XDocument.Parse(xml);
		}

		public static XElement ToXElement(this string xml)
		{
			return XElement.Parse(xml);
		}

		public static XmlDocument ToXmlDocument(this string xml)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(xml);
			return xmlDocument;
		}

		public static XPathNavigator ToXPath(this string xml)
		{
			XPathDocument xPathDocument = new XPathDocument(new StringReader(xml));
			return xPathDocument.CreateNavigator();
		}

		public static byte[] HexStringToBytes(this string value)
		{
			value = value.Replace(" ", "");
			int num = value.Length / 2 - 1;
			byte[] array = new byte[num + 1];
			for (int i = 0; i <= num; i++)
			{
				array[i] = byte.Parse(value.Substring(2 * i, 2), NumberStyles.AllowHexSpecifier);
			}
			return array;
		}

		public static string ToUnicodeString(this string source)
		{
			string text = "";
			if (!string.IsNullOrEmpty(source))
			{
				text = source.Aggregate(text, delegate(string current, char t)
				{
					int num = t;
					return current + "\\u" + num.ToString("x").ToUpper();
				});
			}
			return text;
		}

		public static SecureString ToSecureString(this string value, bool markReadOnly = true)
		{
			if (value.IsNullOrEmpty())
			{
				return null;
			}
			SecureString secureString = new SecureString();
			foreach (char c in value)
			{
				secureString.AppendChar(c);
			}
			if (markReadOnly)
			{
				secureString.MakeReadOnly();
			}
			return secureString;
		}

		public static string ToUnSecureString(this SecureString value)
		{
			if (value == null)
			{
				return null;
			}
			IntPtr intPtr = IntPtr.Zero;
			try
			{
				intPtr = Marshal.SecureStringToGlobalAllocUnicode(value);
				return Marshal.PtrToStringUni(intPtr);
			}
			finally
			{
				Marshal.ZeroFreeGlobalAllocUnicode(intPtr);
			}
		}

		public static string ConvertBinary(this string value, BinaryMode from, BinaryMode to)
		{
			try
			{
				int binary = StringExtensions.GetBinary(from);
				int binary2 = StringExtensions.GetBinary(to);
				int num = Convert.ToInt32(value, binary);
				string text = Convert.ToString(num, binary2);
				if (binary2 == 2)
				{
					switch (text.Length)
					{
					case 7:
						text = "0" + text;
						break;
					case 6:
						text = "00" + text;
						break;
					case 5:
						text = "000" + text;
						break;
					case 4:
						text = "0000" + text;
						break;
					case 3:
						text = "00000" + text;
						break;
					}
				}
				return text;
			}
			catch (Exception)
			{
				return "0";
			}
		}

		private static int GetBinary(BinaryMode mode)
		{
			return mode switch
			{
				BinaryMode.Binary => 2, 
				BinaryMode.Octonary => 8, 
				BinaryMode.DecimalBase => 10, 
				BinaryMode.Hex => 16, 
				_ => 0, 
			};
		}

		public static string ToSbcCase(this string input)
		{
			char[] array = input.ToCharArray();
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] == ' ')
				{
					array[i] = '\u3000';
				}
				else if (array[i] < '\u007f')
				{
					array[i] = (char)(array[i] + 65248);
				}
			}
			return new string(array);
		}

		public static string ToDbcCase(this string input)
		{
			char[] array = input.ToCharArray();
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] == '\u3000')
				{
					array[i] = ' ';
				}
				else if (array[i] > '觐' && array[i] < '｟')
				{
					array[i] = (char)(array[i] - 65248);
				}
			}
			return new string(array);
		}

		public static DateTime ToDateTime(this string timeStamp)
		{
			if (timeStamp.Length > 10)
			{
				timeStamp = timeStamp.Substring(0, 10);
			}
			DateTime dateTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
			long ticks = long.Parse(timeStamp + "0000000");
			TimeSpan value = new TimeSpan(ticks);
			return dateTime.Add(value);
		}

		public static string ToHtmlSafe(this string value)
		{
			return value.ToHtmlSafe(all: false, replace: false);
		}

		public static string ToHtmlSafe(this string value, bool all)
		{
			return value.ToHtmlSafe(all, replace: false);
		}

		public static string ToHtmlSafe(this string value, bool all, bool replace)
		{
			if (value.IsEmpty())
			{
				return string.Empty;
			}
			int[] source = new int[280]
			{
				0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
				10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
				20, 21, 22, 23, 24, 25, 26, 28, 29, 30,
				31, 34, 39, 38, 60, 62, 123, 124, 125, 126,
				127, 160, 161, 162, 163, 164, 165, 166, 167, 168,
				169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
				179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
				189, 190, 191, 215, 247, 192, 193, 194, 195, 196,
				197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
				207, 208, 209, 210, 211, 212, 213, 214, 215, 216,
				217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
				227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
				237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
				247, 248, 249, 250, 251, 252, 253, 254, 255, 256,
				8704, 8706, 8707, 8709, 8711, 8712, 8713, 8715, 8719, 8721,
				8722, 8727, 8730, 8733, 8734, 8736, 8743, 8744, 8745, 8746,
				8747, 8756, 8764, 8773, 8776, 8800, 8801, 8804, 8805, 8834,
				8835, 8836, 8838, 8839, 8853, 8855, 8869, 8901, 913, 914,
				915, 916, 917, 918, 919, 920, 921, 922, 923, 924,
				925, 926, 927, 928, 929, 931, 932, 933, 934, 935,
				936, 937, 945, 946, 947, 948, 949, 950, 951, 952,
				953, 954, 955, 956, 957, 958, 959, 960, 961, 962,
				963, 964, 965, 966, 967, 968, 969, 977, 978, 982,
				338, 339, 352, 353, 376, 402, 710, 732, 8194, 8195,
				8201, 8204, 8205, 8206, 8207, 8211, 8212, 8216, 8217, 8218,
				8220, 8221, 8222, 8224, 8225, 8226, 8230, 8240, 8242, 8243,
				8249, 8250, 8254, 8364, 8482, 8592, 8593, 8594, 8595, 8596,
				8629, 8968, 8969, 8970, 8971, 9674, 9824, 9827, 9829, 9830
			};
			StringBuilder stringBuilder = new StringBuilder();
			foreach (char c in value)
			{
				if (all || source.Contains(c))
				{
					int num = c;
					stringBuilder.Append("&#" + num + ";");
				}
				else
				{
					stringBuilder.Append(c);
				}
			}
			return replace ? stringBuilder.Replace("", "<br />").Replace("\n", "<br />").Replace(" ", "&nbsp;")
				.ToString() : stringBuilder.ToString();
		}

		public static string EncodeBase64(this string value, Encoding encoding = null)
		{
			encoding = encoding ?? Encoding.UTF8;
			byte[] bytes = encoding.GetBytes(value);
			return Convert.ToBase64String(bytes);
		}

		public static string DecodeBase64(this string value, Encoding encoding = null)
		{
			encoding = encoding ?? Encoding.UTF8;
			byte[] bytes = Convert.FromBase64String(value);
			return encoding.GetString(bytes);
		}

		public static byte[] EncryptToBytes(this string value, string pwd)
		{
			ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
			byte[] bytes = aSCIIEncoding.GetBytes(value);
			return StringExtensions.CryptBytes(pwd, bytes, encrypt: true);
		}

		private static byte[] CryptBytes(string pwd, byte[] bytes, bool encrypt)
		{
			TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
			int keySizeBits = 0;
			for (int num = 1024; num >= 1; num--)
			{
				if (tripleDESCryptoServiceProvider.ValidKeySize(num))
				{
					keySizeBits = num;
					break;
				}
			}
			int blockSize = tripleDESCryptoServiceProvider.BlockSize;
			byte[] key = null;
			byte[] iv = null;
			byte[] salt = new byte[14]
			{
				16, 32, 18, 35, 55, 164, 197, 166, 241, 240,
				238, 33, 34, 69
			};
			StringExtensions.MakeKeyAndIv(pwd, salt, keySizeBits, blockSize, ref key, ref iv);
			ICryptoTransform transform = (encrypt ? tripleDESCryptoServiceProvider.CreateEncryptor(key, iv) : tripleDESCryptoServiceProvider.CreateDecryptor(key, iv));
			MemoryStream memoryStream = new MemoryStream();
			CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
			cryptoStream.Write(bytes, 0, bytes.Length);
			try
			{
				cryptoStream.FlushFinalBlock();
			}
			catch (CryptographicException)
			{
			}
			byte[] result = memoryStream.ToArray();
			try
			{
				cryptoStream.Close();
			}
			catch (CryptographicException)
			{
			}
			memoryStream.Close();
			return result;
		}

		private static void MakeKeyAndIv(string pwd, byte[] salt, int keySizeBits, int blockSizeBits, ref byte[] key, ref byte[] iv)
		{
			Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(pwd, salt, 1234);
			key = rfc2898DeriveBytes.GetBytes(keySizeBits / 8);
			iv = rfc2898DeriveBytes.GetBytes(blockSizeBits / 8);
		}

		public static string DecryptFromBytes(this byte[] value, string pwd)
		{
			byte[] bytes = StringExtensions.CryptBytes(pwd, value, encrypt: false);
			ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
			return new string(aSCIIEncoding.GetChars(bytes));
		}

		public static string EncryptToString(this string value, string pwd)
		{
			return value.EncryptToBytes(pwd).ToString();
		}

		public static string DecryptFromString(this string value, string pwd)
		{
			ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
			byte[] bytes = aSCIIEncoding.GetBytes(value);
			return bytes.DecryptFromBytes(pwd);
		}

		public static bool IsNullOrWhiteSpace(this string str)
		{
			return string.IsNullOrWhiteSpace(str);
		}

		public static string FirstToUpper(this string str)
		{
			return str.IsNullOrWhiteSpace() ? string.Empty : (str[0].ToString().ToUpper() + str.Substring(1));
		}

		public static string FirstToLower(this string str)
		{
			return str.IsNullOrWhiteSpace() ? string.Empty : (str[0].ToString().ToLower() + str.Substring(1));
		}

		public static string SetLengthToUpperByStart(this string str, int length = 1)
		{
			if (string.IsNullOrWhiteSpace(str))
			{
				return string.Empty;
			}
			str = str.Trim();
			if (length >= str.Length)
			{
				return str.ToUpper();
			}
			string text = string.Empty;
			for (int i = 0; i < length; i++)
			{
				text += str[i].ToString().ToUpper();
			}
			return text + str.Substring(length);
		}
	}
}
